import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'dart:ui' as dart_ui;

class CustomView extends CustomPainter {
  final Color _color;
  late final Paint _paint;
  late final Paint _CirclePaint;

  dart_ui.Image? _iamge;

  CustomView(this._color, {dart_ui.Image? image}) {
    _iamge = image;
    _CirclePaint = Paint()
      ..color = _color //画笔颜色
      ..strokeCap = StrokeCap.round //画笔笔触类型
      ..isAntiAlias = true //是否启动抗锯齿
      // ..blendMode = BlendMode.multiply //颜色混合模式
      ..style = PaintingStyle.stroke //绘画风格，默认为填充
      // ..colorFilter = const ColorFilter.mode(Colors.blueAccent,
      //     BlendMode.srcIn) //颜色渲染模式，一般是矩阵效果来改变的,但是flutter中只能使用颜色混合模式
      // ..maskFilter =MaskFilter.blur(BlurStyle.inner, 5.0)
      // const MaskFilter.blur(BlurStyle.inner, 3.0) //模糊遮罩效果，flutter中只有这个
      ..filterQuality = FilterQuality.high //颜色渲染模式的质量
      ..strokeWidth = 15.0; //画笔的宽度
    _paint = Paint()
      ..color = Colors.purple //画笔颜色
      ..strokeCap = StrokeCap.round //画笔笔触类型
      ..isAntiAlias = true //是否启动抗锯齿
      // ..blendMode = BlendMode.srcOut //颜色混合模式
      ..style = PaintingStyle.stroke //绘画风格，默认为填充
      // ..colorFilter = const ColorFilter.mode(Colors.blueAccent,
      //     BlendMode.srcIn) //颜色渲染模式，一般是矩阵效果来改变的,但是flutter中只能使用颜色混合模式
      // ..maskFilter =MaskFilter.blur(BlurStyle.inner, 5.0)
      // const MaskFilter.blur(BlurStyle.inner, 3.0) //模糊遮罩效果，flutter中只有这个
      ..filterQuality = FilterQuality.high //颜色渲染模式的质量
      ..strokeWidth = 15.0; //画笔的宽度
  }

  @override
  void paint(Canvas canvas, Size size) {
    if (size.width > 0 && size.height > 0) {
      canvas.clipRect(Rect.fromLTRB(0, 0, size.width, size.height));
    }
    Path path = Path()
      ..moveTo(50, 50)
      ..lineTo(100, 250)
      ..lineTo(300, 250)
      ..lineTo(50, 50);
    canvas.drawPath(path, _paint);
    canvas.drawCircle(const Offset(100, 100), 20, _CirclePaint);
    // canvas.drawPaint(_paint);
    ParagraphBuilder pb = ParagraphBuilder(ParagraphStyle(
        textAlign: TextAlign.left,
        // 对齐方式
        fontWeight: FontWeight.w600,
        // 粗体
        fontStyle: FontStyle.normal,
        // 正常 or 斜体
        fontSize: 18,
        maxLines: 2,
        ellipsis: '...',
        height: 1.1))
      ..pushStyle(dart_ui.TextStyle(color: Colors.blueGrey))
      ..addText("测试文本的绘制测试文本的绘制测试文本的绘制测试文本的绘制")
      ..pop();
    Paragraph paragraph = pb.build()
      ..layout(const ParagraphConstraints(width: 120));
    canvas.drawParagraph(paragraph, const Offset(10, 100));
    if (_iamge != null) {
      canvas.drawImageRect(
          _iamge!,
          Rect.fromLTRB(0, 0, _iamge!.width.toDouble(), _iamge!.height.toDouble()),
          const Rect.fromLTRB(180, 180, 202, 202),
          _paint);
    }
  }

  @override
  bool shouldRepaint(covariant CustomPainter oldDelegate) {
    return true;
  }
}
